package com.ticket.sass.admin.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ticket.sass.common.entity.tenant.OrderTicketEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ywb
 * @since 2024-03-07
 */
public interface OrderTicketMapper extends BaseMapper<OrderTicketEntity> {

    @Select("""
        SELECT
            o.id AS order_id,
            o.order_no,
            o.pay_price,
            o.pay_status,
            o.pay_time,
            o.pay_type,
            o.use_coupon,
            o.created_at AS order_created_at,
            ot.*,
            s.start_time AS session_start_time,
            s.end_time AS session_end_time,
            s.film_name,
            s.hall AS hall_name,
            s.lang AS film_lang,
            s.version AS film_version
        FROM
            orders o
        LEFT JOIN
            order_tickets ot ON o.id = ot.order_id
        LEFT JOIN
            film_schedules s ON ot.film_schedule_id = s.id
         ${ew.customSqlSegment}
        """)
    IPage<OrderTicketEntity> queryPage(Page<OrderTicketEntity> page, @Param(Constants.WRAPPER) QueryWrapper<OrderTicketEntity> queryWrapper);
    
    @Select("""
            SELECT ot.film_schedule_id, SUM(ot.ticket_count) AS ticket_count, SUM(o.pay_price) AS price
            FROM order_tickets ot
            JOIN orders o ON ot.order_id = o.id
            WHERE o.pay_status = 2
            AND ot.cinema_id = #{cinemaId}
            AND o.pay_time BETWEEN #{startTime} AND #{endTime}
            GROUP BY ot.film_schedule_id
            """)
    List<Map<String, Object>> getFilmSaleStatsWithTimeFilterAndCinemaId(@Param("cinemaId") Long cinemaId, @Param("startTime") LocalDate startTime, @Param("endTime") LocalDate endTime);
    
    
    @Select("""
        SELECT
            o.id AS order_id,
            o.order_no,
            o.pay_price,
            o.pay_status,
            o.pay_time,
            o.pay_type,
            o.use_coupon,
            o.created_at AS order_created_at,
            ot.*,
            s.start_time AS session_start_time,
            s.end_time AS session_end_time,
            s.film_name,
            s.hall AS hall_name,
            s.lang AS film_lang,
            s.version AS film_version
        FROM
            orders o
        LEFT JOIN
            order_tickets ot ON o.id = ot.order_id
        LEFT JOIN
            film_schedules s ON ot.film_schedule_id = s.id
         ${ew.customSqlSegment}
        """)
    List<OrderTicketEntity> orderId(@Param(Constants.WRAPPER) QueryWrapper<OrderTicketEntity> queryWrapper);
}
